package services.common;

import java.util.List;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import models.common.bean.GuarantorCommitment;
import models.common.bean.InformationAssuranceBean;
import models.common.entity.t_information_assurance;
import models.common.entity.t_shareholder_sign;
import models.common.entity.t_user;
import models.common.entity.t_user_credit.AuditResult;
import models.common.entity.t_user_info;
import services.base.BaseService;

import common.enums.IsCompany;
import common.utils.Factory;
import common.utils.PageBean;
import common.utils.ResultInfo;

import daos.common.ShareholderSignDao;

/**
 * 股东签字表service
 * 
 * @author xiaojun
 * @version 2017-5-12 上午11:12:45
 */
public class ShareholderSignService extends BaseService<t_shareholder_sign> {

	protected static ShareholderSignDao shareholderSignDao = Factory
			.getDao(ShareholderSignDao.class);
	protected static UserInfoService userInfoService = Factory
			.getService(UserInfoService.class);

	public ShareholderSignService() {
		super.basedao = shareholderSignDao;
	}
	
	protected static InformationAssuranceService informationAssuranceService = Factory.getService(InformationAssuranceService.class);

	/**
	 * 更新股东表
	 * 
	 * @param t
	 * @return
	 * @author xiaojun
	 * @version 2017-11-14
	 */
	public boolean addShareholderSign(t_shareholder_sign t) {
		return shareholderSignDao.save(t);

	}

	/**
	 * 查询签字进度
	 * 
	 * @param cplid
	 *            额度表id
	 * @param is_sign
	 *            0 未签字 1 已签字
	 * @return
	 * @author xiaojun
	 * @version 2017-6-12
	 */
	public int count(long cplid, int is_sign) {
		return shareholderSignDao.count(cplid, is_sign);
	}

	/**
	 * 签字审核不通过修改签字状态
	 * 
	 * @param contract_id
	 *            合同编号
	 * @return
	 * @author xiaojun
	 * @version 2017-6-12
	 */
	public ResultInfo updateSignStatus( long id ,String pact_no){
		ResultInfo result = new ResultInfo();
		t_shareholder_sign shareholder_sign =  findByID(id);
		int i = shareholderSignDao.updateSignStatus(pact_no,id);
		if(i != 1){
			result.code = -1;
			result.msg = "更新保证签署状态异常!";
			return result;
		}
		t_information_assurance  assurance = informationAssuranceService.findByID(shareholder_sign.assurance_id);
		
		List<t_shareholder_sign> list = findListByColumn(" assurance_id = ?" , shareholder_sign.assurance_id);
		List<t_shareholder_sign> list2 = findListByColumn(" assurance_id = ? and is_sign = 1" , shareholder_sign.assurance_id);
		if(list.size() == list2.size() ){
			assurance.status = InformationAssuranceBean.AuditResult.AUDIT_PASS.code;
			
		}
		boolean save = informationAssuranceService.save(assurance);
		if(!save){
			result.code = -1;
			result.msg = "更新保证承若函签署状态异常!";
			return result;	
		}
		result.code = 1;
		return result;
	}


	/**
	 * 更新签字合同编号
	 * 
	 * @param contract_id
	 *            合同编号
	 * @return
	 * @author xiaojun
	 * @version 2017-6-12
	 */
	public boolean saveContract_id(String contract_id, long cplid) {
		return shareholderSignDao.saveContract_id(contract_id, cplid);
	}

	/**
	 * 获取当前用户的保证人承诺列表
	 * 
	 * @param currPage
	 * @param pageSize
	 * @param user
	 *            当前用户即是保证人
	 * @return
	 * @author LiangQiongfeng
	 * @version 2018-1-21
	 */
	public PageBean<GuarantorCommitment> pageOfUser(int currPage, int pageSize,
			t_user user, int showType, String startDate, String endDate,
			int exports) {
		String shareholder_mobile = null;
		String shareholder_name = user.company_full_name;
		if (user.is_company == IsCompany.IS_PERSON.code) {
			t_user_info user_info = userInfoService
					.findUserInfoByUserId(user.id);
			if (user_info != null) {
				shareholder_name = user_info.reality_name;
			}
			shareholder_mobile = user.mobile;
		}
		if(StringUtils.isBlank(shareholder_name) && StringUtils.isBlank(shareholder_name)){
			return null;
		}
		return shareholderSignDao.pageOfUser(currPage, pageSize,
				shareholder_name, shareholder_mobile, showType, startDate,
				endDate, exports);
	}

	/**
	 * 分页查询 保证人信息表-保证人记录
	 * 
	 * @param currPage
	 * @param pageSize
	 * @param assurance_id
	 *            保证信息表ID
	 * @param shareholder_name
	 *            保证人姓名
	 * @param stutas
	 *            签字状态
	 * @return
	 * @author fanghanyong
	 * @createDate 2018年01月25日
	 */
	public PageBean<t_shareholder_sign> pageOfShareHold(int currPage,
			int pageSize, long assurance_id, String shareholder_name,
			int status) {

		return shareholderSignDao.pageOfShareHold(currPage, pageSize,
				assurance_id, shareholder_name, status);
	}
}
